<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${model.packageName}.${model.modulePackage}.${model.modelL}.mapper.${model.model}Mapper">
    
    <resultMap type="${model.model}" id="${model.model}Result">
#foreach ($field in $fieldList)
#if($field.data_type=="datetime")
        <result property="${field.field}" column="${field.columnName}" jdbcType="TIMESTAMP" javaType="java.util.Date"   />
#elseif($field.data_type=="date")
        <result property="${field.field}" column="${field.columnName}" jdbcType="DATE" javaType="java.util.Date"   />
#else
        <result property="${field.field}" column="${field.columnName}" />
#end
#end
#foreach ($field in $fieldList)
#if($field.inputType == "select"&&${field.inputValueTwo} != "")
#if(${field.inputValueOne} == "sys_dept")
        <association property="${field.inputValueOneL}"    column="${field.field}" javaType="Dept" resultMap="${field.inputValueOneL}Result"/>
#else
        <association property="${field.inputValueOneL}"    column="${field.field}" javaType="${field.inputValueOneU}" resultMap="${field.inputValueOneL}Result"/>
#end
#elseif($field.inputType == "select"&&${field.inputValueTwo} == "")
		<association property="sysDictData${velocityCount}"    column="${field.field}" javaType="DictData" resultMap="sysDictData${velocityCount}Result"/>
#end
#end
    </resultMap>
    
#foreach ($field in $fieldList)
#if($field.inputType == "select"&&${field.inputValueTwo} != "")
#if(${field.inputValueOne} == "sys_dept")
    <resultMap id="${field.inputValueOneL}Result" type="Dept">
		<id     property="${field.field}"   column="dept_id"     />
		<result property="${field.inputValueTwoL}" column="${field.inputValueOneL}_${field.inputValueTwo}"   />
	</resultMap>
#else
 	<resultMap id="${field.inputValueOneL}Result" type="${field.inputValueOneU}">
		<id     property="id"   column="${field.inputValueOneL}_id"     />
		<result property="${field.inputValueTwoL}" column="${field.inputValueOneL}_${field.inputValueTwo}"   />
	</resultMap>
#end
#elseif($field.inputType == "select"&&${field.inputValueTwo} == "")
    <resultMap id="sysDictData${velocityCount}Result" type="DictData">
		<id     property="${field.field}"   column="dict_value"     />
		<result property="dictLabel" column="dict_label${velocityCount}"   />
	</resultMap>
#end   
#end  
    
    <select id="select${model.model}List" parameterType="${model.model}" resultMap="${model.model}Result">
        select #foreach($field in $fieldList)
#if(${field.inputType}=="select")
#if(${field.inputValueTwo} != "")
#if(${field.inputValueOne} == "sys_dept")
dic${velocityCount}.dept_id as ${field.inputValueOneL}_id,dic${velocityCount}.${field.inputValueTwo} as ${field.inputValueOneL}_${field.inputValueTwo},s.$field.columnName#if($velocityCount != $fieldList.size()),#end
#else
dic${velocityCount}.id as ${field.inputValueOneL}_id,dic${velocityCount}.${field.inputValueTwo} as ${field.inputValueOneL}_${field.inputValueTwo},s.$field.columnName#if($velocityCount != $fieldList.size()),#end
#end
#else
dic${velocityCount}.dict_label as dict_label${velocityCount},s.$field.columnName#if($velocityCount != $fieldList.size()),#end
#end
#else
s.$field.columnName#if($velocityCount != $fieldList.size()),#end
#end
#end 
 from ${model.tableName} s 
#foreach($field in $fieldList)
#if(${field.inputType}=="select")
#if(${field.inputValueTwo} != "")
#if(${field.inputValueOne} == "sys_dept")
	LEFT JOIN ${field.inputValueOne} dic$velocityCount ON s.${field.columnName}=dic$velocityCount.`dept_id`
#else
	LEFT JOIN ${field.inputValueOne} dic$velocityCount ON s.${field.columnName}=dic$velocityCount.`id`
#end
#else
	LEFT JOIN sys_dict_data dic$velocityCount ON s.${field.columnName}=dic$velocityCount.`dict_value`
#end
#end
#end
        <where>  
#foreach($field in $fieldList)
#if(${field.inputType}=="select"&&${field.inputValueTwo} != "")
#if($field.matchType == 'any')
		<if test="${field.field} != null and ${field.field} != ''">
			AND dic$velocityCount.${field.inputValueTwo} like concat('%', #{${field.field}}, '%')
		</if>
#elseif($field.matchType == 'eq')
		<if test="${field.field} != null and ${field.field} != ''">
			AND dic$velocityCount.${field.inputValueTwo} = #{${field.field}} 
		</if>
#elseif($field.matchType == 'left')
		<if test="${field.field} != null and ${field.field} != ''">
			AND dic$velocityCount.${field.inputValueTwo} like concat('%', #{${field.field}}, '')
		</if>
#elseif($field.matchType == 'right')
		<if test="${field.field} != null and ${field.field} != ''">
			AND dic$velocityCount.${field.inputValueTwo} like concat('', #{${field.field}}, '%')
		</if>
#elseif(${field.matchType}=="numberRange"||${field.matchType}=="dateRange")
		<if test="${field.field}Start != null or ${field.field}End != null">
			AND dic$velocityCount.${field.inputValueTwo} between #{${field.field}Start} and #{${field.field}End}
		</if>
#elseif(${field.matchType} == "select"&&${field.inputValueTwo} != "")
		<if test="${field.field} != null and ${field.field} != ''">
			AND s.${field.columnName} = #{${field.field}} 
		</if>
#end
#else
#if($field.matchType == 'any')
		<if test="${field.field} != null and ${field.field} != ''">
			AND s.${field.columnName} like concat('%', #{${field.field}}, '%')
		</if>
#elseif($field.matchType == 'eq')
		<if test="${field.field} != null and ${field.field} != ''">
			AND s.${field.columnName} = #{${field.field}} 
		</if>
#elseif($field.matchType == 'left')
		<if test="${field.field} != null and ${field.field} != ''">
			AND s.${field.columnName} like concat('%', #{${field.field}}, '')
		</if>
#elseif($field.matchType == 'right')
		<if test="${field.field} != null and ${field.field} != ''">
			AND s.${field.columnName} like concat('', #{${field.field}}, '%')
		</if>
#elseif(${field.matchType}=="numberRange")
		<if test="params.${field.field}Start != null and params.${field.field}Start != ''">
			AND s.${field.columnName} &gt;= #{params.${field.field}Start}
		</if>
		<if test="params.${field.field}End != null and params.${field.field}End != ''">
			AND s.${field.columnName} &lt;= #{params.${field.field}End}
		</if>
#elseif(${field.matchType}=="dateRange")
		<if test="params.${field.field}Start != null and params.${field.field}Start != ''">
			AND date_format(s.${field.columnName},'%y%m%d') &gt;= date_format(#{params.${field.field}Start},'%y%m%d')
		</if>
		<if test="params.${field.field}End != null and params.${field.field}End != ''">
			AND date_format(s.${field.columnName},'%y%m%d') &lt;= date_format(#{params.${field.field}End},'%y%m%d')
		</if>
#elseif(${field.inputValueTwo} == ""&&${field.inputType}=="select")
			AND dic${velocityCount}.dict_type = '${field.inputValue}'
		<if test="${field.field} != null and ${field.field} != ''">
			AND s.${field.columnName} = #{${field.field}} 
		</if>
#elseif(${field.matchType} == "select"&&${field.inputType} == "radio")
		<if test="${field.field} != null and ${field.field} != ''">
			AND s.${field.columnName} = #{${field.field}} 
		</if>
#end
#end
#end
        </where>
    </select>
    <select id="select${model.model}ById" parameterType="Long" resultMap="${model.model}Result">
        select s.* 
#foreach ($field in $fieldList)
#if($field.inputType == "select"&&${field.inputValueTwo} != "")
		,dic${velocityCount}.${field.inputValueTwo} as ${field.inputValueOneL}_${field.inputValueTwo}
#end
#end
		from ${model.tableName} s 
#foreach ($field in $fieldList)
#if($field.inputType == "select"&&${field.inputValueTwo} != "")
#if(${field.inputValueOne} == "sys_dept")
		LEFT JOIN ${field.inputValueOne} dic$velocityCount ON s.${field.columnName}=dic$velocityCount.`dept_id`
#else
		LEFT JOIN ${field.inputValueOne} dic$velocityCount ON s.${field.columnName}=dic$velocityCount.`id`
#end
#end
#end
        where s.id = #{id}
    </select>
    <select id="selectAll" resultMap="${model.model}Result">
        select s.* 
#foreach ($field in $fieldList)
#if($field.inputType == "select"&&${field.inputValueTwo} != "")
		,dic${velocityCount}.${field.inputValueTwo} as ${field.inputValueOneL}_${field.inputValueTwo}
#end
#end
		from ${model.tableName} s 
#foreach ($field in $fieldList)
#if($field.inputType == "select"&&${field.inputValueTwo} != "")
#if(${field.inputValueOne} == "sys_dept")
		LEFT JOIN ${field.inputValueOne} dic$velocityCount ON s.${field.columnName}=dic$velocityCount.`dept_id`
#else
		LEFT JOIN ${field.inputValueOne} dic$velocityCount ON s.${field.columnName}=dic$velocityCount.`id`
#end
#end
#end
    </select>
#foreach($field in $fieldList)
#if($field.existsMethod)
    <select id="check${field.fieldUp}Unique" resultType="int">
		select count(*) from ${model.tableName} where ${field.field}=#{${field.field}} and id != #{id}
	</select>
#end
#if($field.findBy&&$field.field!="id")
    <select id="select${model.model}By${field.fieldUp}" parameterType="String" resultMap="${model.model}Result">
        select s.* 
#foreach ($field in $fieldList)
#if($field.inputType == "select"&&${field.inputValueTwo} != "")
		,dic${velocityCount}.${field.inputValueTwo} as ${field.inputValueOneL}_${field.inputValueTwo}
#end
#end
		from ${model.tableName} s 
#foreach ($field in $fieldList)
#if($field.inputType == "select"&&${field.inputValueTwo} != "")
#if(${field.inputValueOne} == "sys_dept")
		LEFT JOIN ${field.inputValueOne} dic$velocityCount ON s.${field.columnName}=dic$velocityCount.`dept_id`
#else
		LEFT JOIN ${field.inputValueOne} dic$velocityCount ON s.${field.columnName}=dic$velocityCount.`id`
#end
#end
#end
        where s.${field.columnName}=#{${field.field}}
    </select>
#end
#end
    <insert id="insert${model.model}" parameterType="${model.model}">
        insert into ${model.tableName} (
#foreach($field in $fieldList)
#if($field.isPk != "Y"&&${field.field}!="createTime"&&${field.field}!="updateTime")
#if($field.type=="String")
			<if test="${field.field} != null and ${field.field} != '' ">$field.columnName,</if>
#else
			<if test="${field.field} != null">$field.columnName,</if> 
#end
#end
#end
			create_time
        )values(
#foreach($field in $fieldList)
#if($field.isPk != "Y")
#if($field.data_type=="datetime")
			<if test="${field.field} != null">#{${field.field},jdbcType=TIMESTAMP},</if>
#elseif($field.data_type=="date")
			<if test="${field.field} != null ">#{${field.field},jdbcType=DATE},</if>
#elseif($field.data_type=="int")
			<if test="${field.field} != null ">#{${field.field}},</if>
#elseif($field.type=="String")
			<if test="${field.field} != null and ${field.field} != ''">#{${field.field}},</if>
#else
			<if test="${field.field} != null">#{${field.field}},</if>
#end
#end			
#end
			sysdate()
		)
    </insert>

    <insert id="batchAdd" parameterType="${model.model}">
        insert into ${model.tableName} (
#foreach($field in $fieldList)
#if($field.isPk != "Y"&&${field.field}!="createTime"&&${field.field}!="updateTime")
#if($field.type=="String")
		$field.columnName,
#else
		$field.columnName,
#end
#end
#end
		create_time
        )values
        <foreach item="item" index="index" collection="list" separator=",">
        (
#foreach($field in $fieldList)
#if($field.isPk != "Y"&&${field.field}!="createTime"&&${field.field}!="updateTime")
#if($field.data_type=="datetime")
		#{item.${field.field},jdbcType=TIMESTAMP},
#elseif($field.data_type=="date")
		#{item.${field.field},jdbcType=DATE},
#elseif($field.data_type=="int")
		#{item.${field.field}},
#elseif($field.type=="String")
		#{item.${field.field}},
#else
		#{item.${field.field}},
#end
#end			
#end
		sysdate()
		)
		</foreach>
    </insert>
    	 
    <update id="update${model.model}" parameterType="${model.model}">
        update ${model.tableName} 
        <set>
#foreach($field in $fieldList)
#if($field.isPk != "Y"&&${field.field}!="createTime"&&${field.field}!="updateTime")
#if($field.data_type=="datetime")
            <if test="${field.field} != null">$field.columnName = #{${field.field},jdbcType=TIMESTAMP},</if>
#elseif($field.data_type=="date")
            <if test="${field.field} != null">$field.columnName = #{${field.field},jdbcType=DATE},</if>
#elseif($field.data_type=="int")
            <if test="${field.field} != null">$field.columnName = #{${field.field}},</if>
#elseif($field.type=="String")
			<if test="${field.field} != null and ${field.field} != ''">$field.columnName = #{${field.field}},</if>
#else
            <if test="${field.field} != null">$field.columnName = #{${field.field}},</if>
#end
#end
#end
			update_time = sysdate()
        </set>
        where id = #{id}
    </update>
	
    <update id="batchUpdate" parameterType="java.util.List">
        update ${model.tableName} 
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="status =case" suffix="end,">
	            <foreach collection="list" item="item" index="index">
#foreach($field in $fieldList)
#if($field.isPk != "Y"&&${field.field}!="createTime"&&${field.field}!="updateTime")
#if($field.data_type=="datetime")
            		<if test="${field.field} != null">when id=#{item.id} then #{${field.field},jdbcType=TIMESTAMP}</if>
#elseif($field.data_type=="date")
            		<if test="${field.field} != null">when id=#{item.id} then #{${field.field},jdbcType=DATE}</if>
#elseif($field.data_type=="int")
            		<if test="${field.field} != null">when id=#{item.id} then #{${field.field}}</if>
#elseif($field.type=="String")
					<if test="${field.field} != null and ${field.field} != ''">when id=#{item.id} then #{${field.field}}</if>
#else
           			<if test="${field.field} != null">when id=#{item.id} then #{${field.field}}</if>
#end
#end
#end
       		</foreach>
        </trim>
     </trim>
     where id in
     <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
         #{item.id,jdbcType=BIGINT}
     </foreach>
   </update>
	
    <delete id="delete${model.model}ByIds" parameterType="String">
        delete from ${model.tableName} where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
        #{id}
        </foreach>
    </delete>
    
</mapper>